package cep.web.person.controller;

import cep.model.entity.core.Floor;
import cep.model.entity.core.Person;
import cep.model.dao.CoreDao;
import cep.web.person.beans.FloorBean;
import cep.web.person.beans.PersonBean;
import cep.web.person.mapper.PersonMapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * User: golovin
 * Date: 23.06.13
 * Time: 1:37
 */
@Controller
public class PersonController {
    @Autowired
    private FloorBean floorBean;
    @Autowired
    private PersonBean personBean;
    @Autowired
    @Qualifier("commonDaoImplMock")
    private CoreDao coreDao;
    @Autowired
    private PersonMapper personMapper;

    private final Log LOG = LogFactory.getLog(getClass());

    @RequestMapping(value = "/floor")
    public String handleFloor() {
        int id = coreDao.getFloorList().get(0).getId();
        return "redirect:/floor/" + id;
    }

    @RequestMapping(value = "/floor/{id}")
    public ModelAndView handleFloor(@PathVariable String id) {
        LOG.info("Returning floor view " + id);
        ModelAndView mvc = new ModelAndView("floor");
        Integer currentFloor;
        if (id != null) {
            try {
                currentFloor = Integer.parseInt(id);
            } catch (NumberFormatException e) {
                currentFloor = 1;
            }
        } else {
            currentFloor = 1;
        }
        floorBean.setCurrentFloor(currentFloor);
        floorBean.setCurrentPerson(0);

        personMapper.mapFloor(floorBean);
        floorBean.setOneClickToPerson(false);
        mvc.addObject("floorBean", floorBean);
        return mvc;
    }

    @RequestMapping(value = "/person/{id}")
    public ModelAndView handlePerson(@PathVariable String id) {
        LOG.info("Returning person view " + id);
        ModelAndView mvc = new ModelAndView("person");
        personMapper.mapFloor(floorBean);
        floorBean.setOneClickToPerson(true);

        Integer personId;
        if (id != null) {
            try {
                personId = Integer.parseInt(id);
            } catch (NumberFormatException e) {
                personId = 2;
            }
        } else {
            personId = 2;
        }
        floorBean.setCurrentPerson(personId);
        personBean.getBase().setId(personId);
        personMapper.mapPerson(personBean);

        // hack: init current floor
        if (floorBean.getCurrentFloor() == 0) {
            for (Floor f: coreDao.getFloorList()) {
                for (Person p: f.getPersonList()) {
                    if (p.getId() == personId) {
                        floorBean.setCurrentFloor(f.getId());
                        break;
                    }
                }
            }
        }

        mvc.addObject("floorBean", floorBean);
        mvc.addObject("personBean", personBean);
        return mvc;
    }
}
